<template>
	<view class="add-content">
		<view class="form-content">
			<uni-forms ref="customForm" :modelValue="formData" label-width="85px">
				<uni-forms-item label="活动名称" name="accessory">
					<uni-easyinput v-model="formData.activity_name" placeholder="请输入活动名称" />
				</uni-forms-item>
				<uni-forms-item label="开始时间" name="title">
					<uni-datetime-picker type="datetime" :clear-icon="false" v-model="formData.begin_time" />
				</uni-forms-item>
				<uni-forms-item label="结束时间" name="title">
					<uni-datetime-picker type="datetime" :clear-icon="false" v-model="formData.end_time" />
				</uni-forms-item>
				<uni-forms-item label="活动学时" name="title">
					<uniSelect :result="hours" @onSelect="onSelectHours" :title="'请选择活动学时'"></uniSelect>
				</uni-forms-item>
				<uni-forms-item label="学段" name="title">
					<uniSelect :result="period" :title="'请选择学段'" @onSelect="onSelectPeriod"></uniSelect>
				</uni-forms-item>
				<uni-forms-item label="年级" name="title">
					<uniSelect :result="grade" @onSelect="onSelectGrade" :title="'请选择年级'"></uniSelect>
				</uni-forms-item>
				<uni-forms-item label="学科" name="title">
					<uniSelect :result="subject" @onSelect="onSelectSubject" :title="'请选择学科'"></uniSelect>
				</uni-forms-item>
				<uni-forms-item label="活动地点" name="subject">
					<school :selectType="'0'" :text="'活动地点'" @onSelect="getSelectAddress"></school>
				</uni-forms-item>
				<uni-forms-item label="授课老师" name="title">
					<uni-easyinput v-model="formData.grant_teacher" placeholder="请输入授课老师" />
				</uni-forms-item>
				<uni-forms-item label="活动级别" name="title">
					<uniSelect :result="activity_level" @onSelect="onSelectLevel" :title="'请选择活动级别'"></uniSelect>
				</uni-forms-item>
				<uni-forms-item label="评价量表" name="title">
					<uniSelect :result="scale" @onSelect="onSelectScale" :title="'请选择评价量表'"></uniSelect>
				</uni-forms-item>
				<uni-forms-item label="下发教员" name="title">
					<school :selectType="'1'" :text="'下发教员'" @onSelect="getSelectTeacher"></school>
				</uni-forms-item>
				<uni-forms-item label="" name="accessory">
					<uni-file-picker fileMediatype="image" @select="onUpload" limit="9"></uni-file-picker>
				</uni-forms-item>
			</uni-forms>
			<view class="submit-btn"><u-button @click="addClick" type="primary">立即添加</u-button></view>
		</view>
	</view>
</template>

<script>
	import school from "@/pages/common/school/index"
	import uniSelect from "@/components/uni-selected/uni-select"
	import {
		getScale,
		addClass
	} from '@/api/home'
	import config from '@/config'
	const baseUrl = config.baseUrl
	export default {
		props: {
			title: {
				type: String,
				default: ''
			}
		},
		data() {
			return {
				period: this.$store.state.teach.period,
				subject: this.$store.state.teach.subject,
				hours: [{
						value: "4",
						text: "4"
					}, {
						value: "8",
						text: "8"
					}, {
						value: "12",
						text: "12"
					},
					{
						value: "16",
						text: "16"
					}, {
						value: "20",
						text: "20"
					}, {
						value: "24",
						text: "24"
					},
				],
				activity_level: [{
						value: "国家级",
						text: "国家级"
					}, {
						value: "省市级",
						text: "省市级"
					},
					{
						value: "区县级",
						text: "区县级"
					}, {
						value: "学区学片",
						text: "学区学片"
					},
				],
				scale: [],
				grade: [],
				formData: {
					accessory: "",
					activityId: "0",
					activity_name: "", // 活动名称
					grant_teacher: "", //授课老师
					begin_time: "", // 开始时间
					end_time: "", // 结束时间
					activity_level: "", // 活动级别
					activity_add: "", // 活动地点
					activity_add_code: "", // 活动行政区划
					activity_stages: "", // 学段
					activity_grade: "", // 年级
					activity_subject: "", // 学科
					scale_id: "", // 量表id
					hours: "", // 学时
					type: "", // 下发教员类0选择老师，1选择区域
					teachers: "", // 下发教员id数组，逗号分隔
					havepic: "-1",
					deviceId: this.$store.state.user.deviceId
				}
			}
		},
		components: {
			school,
			uniSelect
		},
		methods: {
			//添加按钮事件
			addClick() {
				uni.showLoading({
					title: '正在保存'
				})
				uni.uploadFile({
					url: baseUrl + '/activity/saveLectureActivity',
					filePath: this.formData.accessory,
					name: 'file',
					formData: this.formData,
					success: (res) => {
						if (res.statusCode == 200) {
							uni.$u.toast('保存成功');
							setTimeout(() => {
								this.$tab.reLaunch('/pages/index')
							}, 2000);
						}
					}
				});
			},
			//获取选择的地点
			getSelectAddress(data) {
				this.formData.activity_add_code = data.schoolCode;
				this.formData.activity_add = data.school;
			},
			//选择老师
			getSelectTeacher(data) {
				this.formData.teachers = data.teacher
				this.formData.type = data.type
			},
			//选择学时
			onSelectHours(data) {
				this.formData.hours = data.value
			},
			//选择学段
			onSelectPeriod(data) {
				this.activity_grade = [];
				this.formData.activity_stages = data.value;
				data.grade.forEach(item => {
					this.grade.push({
						text: item,
						value: item
					})
				})
			},
			//选择年级
			onSelectGrade(data) {
				this.formData.activity_grade = data.value;
			},
			//选择学科
			onSelectSubject(data) {
				this.formData.activity_subject = data.value;
				let params = {
					stages: "1",
					deviceId: this.$store.state.user.deviceId,
					subject_id: this.formData.activity_subject
				}
				getScale(params).then(res => {
					res.scales.forEach(item => {
						this.scale.push({
							value: item.id,
							text: item.scale_name
						})
					})
				})
			},
			//选择活动级别
			onSelectLevel(data) {
				this.formData.activity_level = data.value;
			},
			//选择评价量表
			onSelectScale(data) {
				this.formData.scale_id = data.value
			},
			onUpload(e) {
				this.formData.havepic = "1"
				this.formData.accessory = e.tempFilePaths[0];
			},
		}
	}
</script>

<style lang="scss">
	pages {
		background: #fff;
	}

	.add-content {
		background: #fff;

		.form-content {
			padding: 15px;
			height: 93vh;
			overflow-y: scroll;
		}
	}
</style>